package com.tn.controller.homepage;

import com.tn.controller.alarm.common.constants.Constants;
import com.tn.controller.alarm.common.enums.CommonCodeEnum;
import com.tn.controller.alarm.common.model.CommonVO;
import com.tn.controller.alarm.common.model.TextAndValueVO;
import com.tn.controller.alarm.common.model.homepage.vo.WarningCarDataVO;
import com.tn.controller.alarm.common.model.statistics.vo.CurrentTruckLoadingStatisticsVO;
import com.tn.controller.alarm.common.model.statistics.vo.LoadCarStatisticVO;
import com.tn.service.homepage.CommonPageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/common/page/")
@Slf4j
public class CommonPageController {
    @Autowired
    private CommonPageService service;
    /**
     * 获取当前货车运行中数量统计
     * @return
     */
    @RequestMapping("getCurrentTruckLoadingStatistics")
    public CommonVO<CurrentTruckLoadingStatisticsVO> getCurrentTruckLoadingStatistics() {
        CommonVO<CurrentTruckLoadingStatisticsVO> commonVO = new CommonVO<>();
        try {
            CurrentTruckLoadingStatisticsVO currentTruckLoadingStatisticsVO = service.getCurrentTruckLoadingStatistics();
            commonVO.setData(currentTruckLoadingStatisticsVO);
        } catch (Exception e) {
            commonVO.setCode(CommonCodeEnum.EXPIRE.getCode());
            commonVO.setMsg(CommonCodeEnum.EXPIRE.getMessage());
            log.error("getCurrentTruckLoadingStatistics error:{}",e.getMessage(),e);
        }
        return commonVO;
    }

    /**
     * 获取货车离场数量
     * @return
     */
    @RequestMapping("getTruckLeaveCount")
    public CommonVO<Integer> getTruckLeaveCount() {
        CommonVO<Integer> commonVO = new CommonVO<>();
        try {
            int count = service.getTruckLeaveCount();
            commonVO.setData(count);
        } catch (Exception e) {
            commonVO.setCode(CommonCodeEnum.EXPIRE.getCode());
            commonVO.setMsg(CommonCodeEnum.EXPIRE.getMessage());
            log.error("getTruckLeaveCount error:{}",e.getMessage(),e);
        }
        return commonVO;
    }

    /**
     * 获取货车工作时长统计
     * @return
     */
    @RequestMapping("getTruckWorkTimeStatistic")
    public CommonVO<List<TextAndValueVO<String,Integer>>> getTruckWorkTimeStatistic() {
        CommonVO<List<TextAndValueVO<String,Integer>>> commonVO = new CommonVO<>();
        try {
            List<TextAndValueVO<String,Integer>> textAndValueVOList = service.getTruckWorkTimeStatistic();
            commonVO.setData(textAndValueVOList);
        } catch (Exception e) {
            commonVO.setCode(CommonCodeEnum.EXPIRE.getCode());
            commonVO.setMsg(CommonCodeEnum.EXPIRE.getMessage());
            log.error("getTruckWorkTimeStatistic error:{}",e.getMessage(),e);
        }
        return commonVO;
    }

    /**
     * 获取装载车数量统计
     * @return
     */
    @RequestMapping("getLoadCarStatistic")
    public CommonVO<LoadCarStatisticVO> getLoadCarStatistic() {
        CommonVO<LoadCarStatisticVO> commonVO = new CommonVO<>();
        try {
            LoadCarStatisticVO loadCarStatistic = service.getLoadCarStatistic();
            commonVO.setData(loadCarStatistic);
        } catch (Exception e) {
            commonVO.setCode(CommonCodeEnum.EXPIRE.getCode());
            commonVO.setMsg(CommonCodeEnum.EXPIRE.getMessage());
            log.error("getLoadCarStatistic error:{}",e.getMessage(),e);
        }
        return commonVO;
    }

    /**
     * 获取告警货车信息
     * @return
     */
    @RequestMapping("getWarnTruckList")
    public CommonVO<List<WarningCarDataVO>> getWarnTruckList() {
        CommonVO<List<WarningCarDataVO>> commonVO = new CommonVO<>();
        try {
            List<WarningCarDataVO> warnTruckList = service.getWarnCarList(Constants.TRUCK);
            commonVO.setData(warnTruckList);
        } catch (Exception e) {
            commonVO.setCode(CommonCodeEnum.EXPIRE.getCode());
            commonVO.setMsg(CommonCodeEnum.EXPIRE.getMessage());
            log.error("getWarnTruckList error:{}",e.getMessage(),e);
        }
        return commonVO;
    }

    /**
     * 获取告警装载车列表
     * @return
     */
    @RequestMapping("getWarnLoadCarList")
    public CommonVO<List<WarningCarDataVO>> getWarnLoadCarList() {
        CommonVO<List<WarningCarDataVO>> commonVO = new CommonVO<>();
        try {
            List<WarningCarDataVO> warnVehicleList = service.getWarnCarList(Constants.VEHICLE);
            commonVO.setData(warnVehicleList);
        } catch (Exception e) {
            commonVO.setCode(CommonCodeEnum.EXPIRE.getCode());
            commonVO.setMsg(CommonCodeEnum.EXPIRE.getMessage());
            log.error("getWarnLoadCarList error:{}",e.getMessage(),e);
        }
        return commonVO;
    }

    /**
     * 获取货场所有货物种类统计
     * @return
     */
    @RequestMapping("getAllGoodsTypeStatistic")
    public CommonVO<List<TextAndValueVO<String,Float>>> getAllGoodsTypeStatistic() {
        CommonVO<List<TextAndValueVO<String,Float>>> commonVO = new CommonVO<>();
        try {
            List<TextAndValueVO<String,Float>> textAndValueVOList = service.getAllGoodsTypeStatistic();
            commonVO.setData(textAndValueVOList);
        } catch (Exception e) {
            commonVO.setCode(CommonCodeEnum.EXPIRE.getCode());
            commonVO.setMsg(CommonCodeEnum.EXPIRE.getMessage());
            log.error("getAllGoodsTypeStatistic error:{}",e.getMessage(),e);
        }
        return commonVO;
    }
}
